import java.util.Arrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Demo6715 {

  public static void main(String[] args) {
    //使用自然排序
	Course6715[] courses=getCourses();
    Arrays.sort(courses);
    for (Course6715 c : courses) {
      System.out.println(c);
    }
	//使用Stream
	Arrays.stream(getCourses())
	.sorted((c1,c2)->c1.getTerm().compareToIgnoreCase(c2.getTerm()))
	.forEach(System.out::println);
	
	System.out.println("最低分:"+Arrays.stream(getCourses()).mapToInt(c1->c1.getScore()).min().getAsInt());
	System.out.println("最高分:"+Arrays.stream(getCourses()).mapToInt(c1->c1.getScore()).max().getAsInt());
	System.out.println("平均分:"+Arrays.stream(getCourses()).mapToInt(c1->c1.getScore()).average().getAsDouble());
	
	System.out.println("85分以上：");
	
	Arrays.stream(getCourses())
	.filter(c1->c1.getScore()>85)
	.forEach(System.out::println);
  }
  public static void main(String[] args)  {
		  System.out.println("--------数组：自然排序，默认输出--------");
	    //使用自然排序
	    Course6715[] courses=getCourses();
	    Arrays.sort(courses);
	    for (Course6715 c : courses) {
	      System.out.println(c);
	    }
	    System.out.println("--------集合：自然排序，默认输出--------");
	    //集合
	    List<Course6715> list=new ArrayList<Course6715>();
	    for (Course6715 course6715 : courses) {
			list.add(course6715);
		}
	    list.forEach(System.out::println);
	    System.out.println("--------集合：比较器排序，匿名内部类，默认输出--------");
	    Collections.sort(list, new Comparator<Course6715>() {

			@Override
			public int compare(Course6715 o1, Course6715 o2) {
				int num=o1.getTerm().compareTo(o2.getTerm());
				if(num==0) {
					//调用重写的equals方法
					if(o1.equals(o2)) {
						return 0;
					}else {
						return 1;
					}
				}	
				return num;
			}
		});
	    list.forEach(System.out::println);
	    
	    System.out.println("--------集合：比较器排序，Lambda表达式，默认输出--------");
	    list.sort((Course6715 o1, Course6715 o2)->o2.getScore()-o1.getScore());
	    list.forEach(System.out::println);
	  }

	  private static Course6715[] getCourses() {
	    //使用数组保存一组对象
	    Course6715[] courses=new Course6715[10];
	    courses[0]=new Course6715("A001", "C程序设计基础", "1上", 4, 80);
	    courses[1]=new Course6715("B002", "Pthon程序基础", "1下", 2, 85);
	    courses[2]=new Course6715("A002", "面向对象程序设计", "2上", 3, 78);
	    courses[3]=new Course6715("C001", "Java程序高级开发", "3上", 2, 83);
	    courses[4]=new Course6715("C002", "软件工程与UML", "3下", 3, 75);
	    courses[5]=new Course6715("B001", "操作系统", "2下", 3, 90);
	    courses[6]=new Course6715("A003", "数据库原理", "2上", 3, 78);
	    courses[7]=new Course6715("B003", "网络基础", "3上", 3, 75);
	    courses[8]=new Course6715("0001", "大学英语", "1上", 3, 80);
	    courses[9]=new Course6715("0002", "高等数学", "1下", 2, 85);
	    return courses;
	  }
}
